package com.web.surf.sample.eshop.faces.backing.beans;

import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashSet;
import javax.faces.event.AjaxBehaviorEvent;
import com.web.surf.sample.eshop.database.manager.services.OrderServiceImpl;
import com.web.surf.sample.eshop.faces.backing.validators.FormValidator;
import com.web.surf.sample.eshop.faces.managed.beans.OrderBean;
import com.web.surf.sample.eshop.utils.MessageProvider;

public class OrderBacking implements Serializable {

	private static final long serialVersionUID = -2623184704263695496L;
	private LinkedHashSet<String> defaultCountries;
	private LinkedHashSet<String> defaultStreetKinds;
	private boolean formCorrect;
	private String successMessage = "";
	private boolean countryLogic;
	private String country;
	private String otherCountry;
	// Spring beans
	private OrderBean orderBean;
	private OrderServiceImpl orderService;


	public void confirmAddress(AjaxBehaviorEvent e) {
		if (FormValidator.validateAddressBean(orderBean.getAddressBean())) {
			formCorrect=true;
		}		
	}
	
	public HashSet<String> getDefaultCountries() {
		defaultCountries = new LinkedHashSet<String>();
		if (defaultCountries.addAll(MessageProvider.getValues("countries")));
		return defaultCountries;
	}

	public HashSet<String> getDefaultStreetKinds() {
		defaultStreetKinds = new LinkedHashSet<String>();
		if (defaultStreetKinds.addAll(MessageProvider.getValues("streetKinds")));
		return defaultStreetKinds;
	}

	public void makeOrder(AjaxBehaviorEvent e) {
		formCorrect = false;		
		orderBean.setOrderDate(new Date());
		successMessage = "You make your order: "
				+ DateFormat.getInstance().format(orderBean.getOrderDate());
		orderService.insertOrder(orderBean);
		clear();
	}
	
	public void chooseCountry(AjaxBehaviorEvent e) {
		if (country.equals("other")) {			
			countryLogic = true;
		}
		else {	
			orderBean.getAddressBean().setCountry(country);
			countryLogic = false;
		}
	}
		
	public void chooseOtherCountry(AjaxBehaviorEvent e) {
		if (!otherCountry.isEmpty()&&("other".equals(country))) {			
			orderBean.getAddressBean().setCountry(otherCountry);	
		}
	}
	
	public void editAddress(AjaxBehaviorEvent e) {
		formCorrect = false;	
	}

	public void clearOrder(AjaxBehaviorEvent e) {
		clear();
	}
	
	private void clear() {
		formCorrect = false;
		successMessage = "";
		orderBean.clear();		
	}
	
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getOtherCountry() {
		return otherCountry;
	}
	public void setOtherCountry(String otherCountry) {
		this.otherCountry = otherCountry;
	}	
	public boolean isCountryLogic() {
		return countryLogic;
	}

	public boolean isFormCorrect() {
		return formCorrect;
	}

	public String getSuccessMessage() {
		return successMessage;
	}

	// Spring setters
	public void setOrderBean(OrderBean orderBean) {
		this.orderBean = orderBean;
	}

	public void setOrderService(OrderServiceImpl orderService) {
		this.orderService = orderService;
	}

}
